package com.cpi.basilos.silo.converter;

import org.apache.log4j.Logger;

import com.cpi.basilos.location.view.LocationView;
import com.cpi.basilos.silo.dto.SiloEntryMovementsDTO;
import com.cpi.basilos.silo.exception.SiloException;
import com.cpi.basilos.silo.view.SiloEntityMovementsView;
import com.cs.common.exception.CSException;
import com.cs.common.utils.NumberUtil;

public class SiloMovementsConverter {
	private static org.apache.log4j.Logger logger = Logger.getLogger(SiloMovementsConverter.class);
	private static final String FURG_TYPE = "Tolva";//$NON-NLS-1$
	private static final String TRUCK_TYPE = "Camion";//$NON-NLS-1$
	
	public SiloEntityMovementsView convertEntryDTOToView(SiloEntryMovementsDTO siloEntryMovementsDTO)throws SiloException{
		SiloEntityMovementsView siloEntryMovementsView = null;
		LocationView locationView = null;

		try{
			siloEntryMovementsView = new SiloEntityMovementsView();
			
			if (siloEntryMovementsDTO.isFurgKind()){
				siloEntryMovementsView.setShipping(FURG_TYPE);
			}else{
				siloEntryMovementsView.setShipping(TRUCK_TYPE);
			}
			locationView  = new LocationView();
			siloEntryMovementsView.setMovements(siloEntryMovementsDTO.getOperation());
			siloEntryMovementsView.setTanks(NumberUtil.convertInteger(siloEntryMovementsDTO.getTank()));
			locationView.setLocationId(Integer.parseInt(siloEntryMovementsDTO.getLocation()));
			siloEntryMovementsView.setLocationView(locationView);
			siloEntryMovementsView.setAmount(NumberUtil.convertQuantity(siloEntryMovementsDTO.getAmount()));
			siloEntryMovementsView.setCornType(siloEntryMovementsDTO.getCornType());
			siloEntryMovementsView.setWetness(NumberUtil.convertQuantity(siloEntryMovementsDTO.getWetness()));
			siloEntryMovementsView.setOriginAmount(NumberUtil.convertQuantity(siloEntryMovementsDTO.getOriginAmount()));
			siloEntryMovementsView.setOrigin(siloEntryMovementsDTO.getOrigin());
			siloEntryMovementsView.setWagon(siloEntryMovementsDTO.getFurg());
			
		}catch( Exception exception ){
			SiloException siloException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			siloException = new SiloException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloEntryMovementsView;
	}
	
	

}
